package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.MessageFormat;
import java.util.Arrays;

/**
 * @author d11yu
 */
@Controller
public class ParamController {
    @RequestMapping("/testServletAPI")
    public String testServletAPI(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        System.out.println(MessageFormat.format("username: {0}, password: {1}.", username, password));
        return "success";
    }

    @RequestMapping("/testParam")
    public String testParam(
            @RequestParam(value = "user_name", required = false, defaultValue = "admin") String username,
            String password,
            String[] hobbies,
            @RequestHeader("Host") String host,
            @CookieValue("JSESSIONID") String JSESSIONID) {
        System.out.println(MessageFormat.format("username: {0}, password: {1}, hobbies: {2}.", username, password, Arrays.toString(hobbies)));
        System.out.println(MessageFormat.format("Host: {0}", host));
        System.out.println(MessageFormat.format("JSESSIONID: {0}", JSESSIONID));
        return "success";
    }

    @RequestMapping("/testPojoUser")
    public String testPojoUser(User user) {
        System.out.println(user);
        return "success";
    }
}
